﻿@using RadzenBlazorDemos.Data
@using RadzenBlazorDemos.Models.Northwind
@using Microsoft.EntityFrameworkCore
@using RadzenBlazorDemos.Pages

@inherits DbContextPage

<RadzenDataGrid @ref="grid" AllowFiltering="true" AllowColumnResize="true" 
    FilterMode="FilterMode.Simple" PageSize="5" AllowPaging="true" AllowSorting="true" Data="@employees" TItem="Employee" ColumnWidth="300px" 
    FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive"
    LogicalFilterOperator="LogicalFilterOperator.Or" Filter="@OnFilter">
    <Columns>
        <MyCustomDataGridColumn TItem="Employee" Property="EmployeeID" Title="ID" Frozen="true" Width="80px" TextAlign="TextAlign.Center" />
        <RadzenDataGridColumn TItem="Employee" Title="Photo" Sortable="false" Filterable="false" Width="200px" >
            <Template Context="data">
                <RadzenImage Path="@data.Photo" style="width: 40px; height: 40px; border-radius: 8px;" />
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn TItem="Employee" Property="FirstName" Title="First Name" />
        <RadzenDataGridColumn TItem="Employee" Property="LastName" Title="Last Name" Width="150px"/>
        <MyCustomDataGridColumn TItem="Employee" Property="BirthDate" Title="Birth Date" FormatString="{0:d}" />
        <RadzenDataGridColumn TItem="Employee" Property="Country" Title="Country" />
        <RadzenDataGridColumn TItem="Employee" Property="Notes" Title="Notes" />
    </Columns>
</RadzenDataGrid>

@code {
    IEnumerable<Employee> employees;
    RadzenDataGrid<Employee> grid;

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        employees = dbContext.Employees;
    }

    protected override Task OnAfterRenderAsync(bool firstRender)
    {
        if(firstRender)
        {
            var column = grid.ColumnsCollection.Where(c => c.Property == "FirstName").FirstOrDefault();

            if(column != null)
            {
                column.FilterValue = "Nan";
                column.FilterOperator = FilterOperator.StartsWith;
                grid.Reload();
            }
        }

        return base.OnAfterRenderAsync(firstRender);
    }

    void OnFilter(DataGridColumnFilterEventArgs<Employee> args)
    {
    //
    }
}